#include "RoleMgr.h"
#include "User.h"
#include "Monster.h"

using namespace stdext;
using std::make_pair;

CRoleMgr::CRoleMgr()
{}

CRoleMgr::~CRoleMgr()
{
	hash_map<unsigned int, IRole*>::iterator itr;
	IRole* pRole = NULL;

	for (itr = m_hmapRoles.begin(); itr != m_hmapRoles.end(); itr++)
	{
		pRole = itr->second;
		if (pRole)
		{
			delete pRole;
			itr->second = NULL;
		}
	}

	m_hmapRoles.clear();
}

void CRoleMgr::AddUser(int nType, unsigned int unID)
{
	IRole* pRole = (IRole*) new CUser(nType, unID);
	if (pRole)
	{
		m_hmapRoles.insert(make_pair(unID, pRole));
	}
}

IRole* CRoleMgr::GetRole(unsigned int unID)
{
	hash_map<unsigned int, IRole*>::iterator itr = m_hmapRoles.find(unID);
	if (itr != m_hmapRoles.end())
	{
		return itr->second;
	}

	return NULL;
}

void CRoleMgr::AddMonster(int unID)
{
	IRole* pRole = (IRole*) new CMonster(unID);
	if (pRole)
	{
		m_hmapRoles.insert(make_pair(unID, pRole));
	}
}

void CRoleMgr::OnTimer()
{
	IRole* pRole = NULL;

	hash_map<unsigned int, IRole*>::iterator itr;
	for (itr = m_hmapRoles.begin(); itr != m_hmapRoles.end(); itr++)
	{
		pRole = itr->second;
		if (pRole)
		{
			pRole->OnTimer();
		}
	}
}